import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class MainCharacter implements KeyListener, ActionListener{
	public static final double SPEED = 0.0; 
	public static final double ACCEL = .002;
	public static final double FRICTION = .001;
	public static final long SWITCH_TIME = 300;
	private double x = 0;
	private double y = 0;
	private double delX = 0.0;
	private double delY = 0.0;
	private BufferedImage bi1 ;
	private BufferedImage bi2;
	private boolean whichImage = false;
	private boolean[] status = {false, false, false, false};
	long lastTime = 0;
	long timeSinceSwitch = 0;
	public MainCharacter(){
		try {
			bi1 = ImageIO.read(new File("res/feet1.png"));
			bi2 = ImageIO.read(new File("res/feet2.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lastTime = System.currentTimeMillis();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		double vchange = (System.currentTimeMillis() - lastTime);
		double achange = ACCEL * (System.currentTimeMillis() - lastTime);
		lastTime = System.currentTimeMillis(); 
		if(status[0]){
			delX+=achange;
		}
		if(status[1]){
			delX-=achange;
		}
		if(status[2]){
			delY-=achange;
		}
		if(status[3]){
			delY+=achange;
		}
		if(delX > 0){
			delX -= FRICTION * vchange;
			delX = Math.max(delX, 0.0);
		}
		else{
			delX += FRICTION * vchange;
			delX = Math.min(delX, 0.0);
		}
		if(delY > 0){
			delY -= FRICTION * vchange;
			delY = Math.max(delY, 0.0);
		}
		else{
			delY += FRICTION * vchange;
			delY = Math.min(delY, 0.0);
		}
		x+=delX * vchange;
		y+=delY * vchange;
		boolean walking = false;
		for(boolean b: status){
			walking = walking || b;
		}
		if(timeSinceSwitch > SWITCH_TIME && walking){
			timeSinceSwitch = 0;
			whichImage = !whichImage;
		}
		else{
			timeSinceSwitch += vchange;
		}
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode() == KeyEvent.VK_UP){
			status[2] = true;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_DOWN){
			status[3] = true;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_LEFT){
			status[1] = true;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_RIGHT){
			status[0] = true;
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		if(arg0.getKeyCode() == KeyEvent.VK_UP){
			status[2] = false;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_DOWN){
			status[3] = false;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_LEFT){
			status[1] = false;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_RIGHT){
			status[0] = false;
		}
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public Image getImage() {
		return (whichImage)?bi1:bi2;
	}
	
	public double getX(){
		return x;
	}
	public double getY(){
		return y;
	}
	
}
